package dyyx;

import java.util.Date;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

public class RedisUtil {
	
	private static final String server = "127.0.0.1"; 
	
	private static volatile Jedis jedis = null;
	
	
	public synchronized static Jedis getJedis(){
		if(jedis!=null){
			return jedis;
		}
		 Jedis jedisTmp = new  Jedis(server);
		 // jedisTmp.connect();
		 jedis = jedisTmp;
		 return jedis;		
	}
	
	
	
	
	
	public static void main(String[] args) throws Exception {
		
		System.out.println("hello,redis");
		Jedis client = getJedis();
		System.out.println("client="+client);
		// System.out.println(client.asking());
		
		// System.out.println(client.clusterInfo());
		System.out.println(jedis.info());
		

		String key = "listkey";
		jedis.del(key);
		//  head (LPUSH) or tail (RPUSH)
		jedis.rpush(key, "a","b");
		jedis.lpush(key, "c","d");
		
		List<String> list = jedis.lrange(key, 0, 100);
		// [d, c, a, b]
		System.out.println(list);
		
		list = jedis.lrange(key, 0, 2);
		// [d, c, a]
		System.out.println(list);

		
		//String str = jedis.get(key);
		
		// System.out.println(str);
		
		// jedis.zremrangeByScore(key, min, max)

		// jedis.zremrangeByRank(key, start, stop);

		key = "zsetkey";
		Date time = new Date();
		int size = 5;
		double score = 0 - time.getTime();
		jedis.zadd(key, score, time+"");
		
		Long zcard = jedis.zcard(key);
		System.out.println("zcard="+zcard);

		Long zcount = jedis.zcount(key,  Long.MIN_VALUE, Long.MAX_VALUE);
		System.out.println("zcount="+zcount);

		
		Set<Tuple> zsets = jedis.zrangeByScoreWithScores(key,  Long.MIN_VALUE, Long.MAX_VALUE,0,size);
		
		System.out.println(zsets);
		
		int start = size;
		int stop = Integer.MAX_VALUE;
		jedis.zremrangeByRank(key, start, stop);
		
		
		System.out.println("*******");
		
		key = "zsetkey2";
		score = time.getTime();
		jedis.zadd(key, score, time+"");
		
		zcard = jedis.zcard(key);
		System.out.println("zcard="+zcard);

		zcount = jedis.zcount(key,  0, Long.MAX_VALUE);
		System.out.println("zcount="+zcount);

		
		zsets = jedis.zrangeByScoreWithScores(key,  0, Long.MAX_VALUE,0,size);
		
		System.out.println(zsets);
		
		start = 0 - size - 9;
		stop = 0 - size;
		jedis.zremrangeByRank(key, start, stop);
		
		
		

		
		
		
		
		
		
		
		
		
		
		
		

	}
	
	
}
